//
//  VideoViewController.m
//  YLProject
//
//  Created by 王跃 on 2018/1/10.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "VideoViewController.h"
#import "Video_ChatHomeHeaderView.h"
#import <AVFoundation/AVFoundation.h>
@interface VideoViewController ()
@property(nonatomic,strong)Video_ChatHomeHeaderView *headerView;
@property (strong, nonatomic) AVCaptureSession *session;//媒体管理会话
@property (strong, nonatomic) AVCaptureDeviceInput *captureInput;//输入数据对象
@property (strong, nonatomic) AVCaptureVideoDataOutput *videoOutput;//输出数据对象
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *captureLayer;//视频预览图层
@end

@implementation VideoViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"视频聊";
    self.view.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.headerView];
    [self initCapture];
}
/* 初始化摄像头 */
- (void)initCapture{
    //1. 创建媒体管理会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    self.session = session;
    //判断分辨率是否支持1280*720，支持就设置为1280*720
    if( [session canSetSessionPreset:AVCaptureSessionPreset1280x720] ) {
        session.sessionPreset = AVCaptureSessionPreset1280x720;
    }
    //2. 获取后置摄像头设备对象
    AVCaptureDevice *device = nil;
    NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for (AVCaptureDevice *camera in cameras) {
        if (camera.position == AVCaptureDevicePositionFront) {//取得后置摄像头
            device = camera;
        }
    }
    if(!device) {
        NSLog(@"取得后置摄像头错误");
        return;
    }
    //3. 创建输入数据对象
    NSError *error = nil;
    AVCaptureDeviceInput *captureInput = [[AVCaptureDeviceInput alloc] initWithDevice:device
                                                                                error:&error];
    if (error) {
        NSLog(@"创建输入数据对象错误");
        return;
    }
    self.captureInput = captureInput;
    //4. 创建输出数据对象
    _videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    _videoOutput.alwaysDiscardsLateVideoFrames = NO;
    _videoOutput.videoSettings =
    [NSDictionary dictionaryWithObject:
     [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    //5. 添加输入数据对象和输出对象到会话中
    if ([session canAddInput:captureInput]) {
        [session addInput:captureInput];
    }
    if ([session canAddOutput:_videoOutput]) {
        [session addOutput:_videoOutput];
    }
    //6. 创建视频预览图层
    AVCaptureVideoPreviewLayer *videoLayer =
    [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    videoLayer.frame = CGRectMake(0, 0, self.headerView.frame.size.height-30, self.headerView.frame.size.height-30);
    videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    //插入图层在拍照按钮的下方
    [self.headerView.vudioView.layer addSublayer:videoLayer];
    self.captureLayer = videoLayer;
    [self.session startRunning];//开始捕捉
}

-(Video_ChatHomeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [Video_ChatHomeHeaderView headerView];
        _headerView.frame = CGRectMake(0, STATUS_BAR_HEIGHT, Screen_Width, _headerView.frame.size.height + 30);
        _headerView.vudioView.layer.cornerRadius = (_headerView.frame.size.height - 30)/ 2;
    }
    return _headerView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
